
jQuery(function($){
    /**生成一个随机数**/
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min) + min);
    }
    /**生成一个随机色**/
    function randomColor(min, max) {
        var r = randomNum(min, max);
        var g = randomNum(min, max);
        var b= randomNum(min, max);
        return "rgb(" + r + "," + g + "," + b + ")";
    }
    var code=drawPic();
    document.getElementById("vcode_img").onclick = function(e) {
        e.preventDefault();
        code=drawPic();
    }
    /**绘制验证码图片**/
    function drawPic() {
        var canvas = document.getElementById("vcode_img");
        var width = canvas.width;
        var height = canvas.height;
        //获取该canvas的2D绘图环境
        var ctx = canvas.getContext('2d');
        ctx.textBaseline ='bottom';
        /**绘制背景色**/
        ctx.fillStyle = randomColor(180,240);
        //颜色若太深可能导致看不清
        ctx.fillRect(0,0,width,height);
        /**绘制文字**/
        var str ='ABCEFGHJKLMNPQRSTWXY123456789';
        var code="";
        //生成四个验证码
        for(var i=1;i<=4;i++) {
            var txt = str[randomNum(0,str.length)];
            code=code+txt;
            ctx.fillStyle = randomColor(50,160);
            //随机生成字体颜色
            ctx.font = randomNum(15,40) +'px SimHei';
            //随机生成字体大小
            var x =10 +i *25;
            var y = randomNum(25,35);
            var deg = randomNum(-45,45);
            //修改坐标原点和旋转角度
            ctx.translate(x, y);
            ctx.rotate(deg * Math.PI /180);
            ctx.fillText(txt,0,0);
            //恢复坐标原点和旋转角度
            ctx.rotate(-deg * Math.PI /180);
            ctx.translate(-x, -y);
        }


        /**绘制干扰线**/
        for(var i=0;i<3;i++) {
            ctx.strokeStyle = randomColor(40, 180);
            ctx.beginPath();
            ctx.moveTo(randomNum(0,width/2), randomNum(0,height/2));
            ctx.lineTo(randomNum(0,width/2), randomNum(0,height));
            ctx.stroke();
        }
        /**绘制干扰点**/
        for(var i=0;i <50;i++) {
            ctx.fillStyle = randomColor(255);
            ctx.beginPath();
            ctx.arc(randomNum(0, width), randomNum(0, height),1,0,2* Math.PI);
            ctx.fill();
        }
     
      console.log(code)
        return code;
    };
    $("#login").click(function(){
    	//layer.msg("login");
	var uname = $("#uname").val();
	if(uname.length == 0) {
		layer.tips("用户名不能为空!", "#uname", {
			tips: [2, "#FF0000"],
			time: 1000
		});
		return false;
	}
	var upwd = $("#upwd").val();
	if(upwd.length == 0) {
		layer.tips("密码不能为空!", "#upwd", {
			tips: [2, "#FF0000"],
			time: 1000
		});
		return false;
	}
	var patrn = /^(\w){6,20}$/;
	if(!patrn.test(upwd)) {

		layer.tips("密码输入有误！", "#upwd", {
			tips: [2, "#FF0000"],
			time: 1000
		});
		return false;
	}

	var vcode = $("#vcode").val();
	if(vcode.length == 0) {

		layer.tips("验证码不能为空！", "#vcode", {
			tips: [2, "#FF0000"],
			time: 1000
		});
		return false;
	}
	console.log(vcode)
	console.log(code)
	if(vcode.toUpperCase() != code.toUpperCase()) {

		layer.tips("验证码输入有误！", "#vcode", {
			tips: [2, "#FF0000"],
			time: 1000
		});
		return false;
	}
	//验证验证码
	var vcode=$('#vcode').val();
	console.log(uname,upwd,vcode);
	var reg=/^[A-Za-z00-9]{4}$/;
	if(!reg.exec(vcode))
	{
		layer.tips("验证码长度有误或包含非法字符！","vcode",{
			tip:[2,"#FF0000"],
			time:1000
		});
		return false;
	}
	
	
	
	$.ajax({
		url: "dataJson/login.json",
		type: 'get',
		dataType: "json",
    	success: function(result){
    		
    		var userinfo=result.data;
    		var temp=false;
    		$.each(userinfo,function(index,value){
    			if(uname==value.uname&&upwd==value.upwd){
    			temp=true;
    			return;
    		}
    	});
    			
    		
    	
    		if(temp==false)
    		
    			layer.msg("登录失败！",{icon:2,time:2000});
    		
    		else
    		{
    			var state=$("#rememberPwd").prop("checked");
    			if(state==true){
    				localStirage.setItem("uname",uname);
    				localStorage.setItem("upwd",upwd);
    			}else{
    				localStorage.clear();
    			}
    			sessionStorage.setItem("uname",uname);
    			window.localtion.href='manager.html';
    		}   		
    		},	
    		error:function(){
    			layer.msg("请求服务器失败！",{icon:2,time:2000});
    		}
		});
	});
          return true;
    })


    
    jQuery.ajax({
    	url:"dataJSON/loginregister.json",
    	data:{},
    	type:"get",
    	success:function(result){
    		var temp=result.code;
    		if(temp==1){
    			window.location.href="login new.html";
    		}else{
    			layer.msg("注册失败！",{icon:2});
    		}
    	},
    	error:function(){
    		layer.msg("访问服务器失败！",{icon:2});
    	}
    });



function reset() {
	$("#frm_login")[0].reset();

}
function login_register(){
	var uname=$('#r_uname').val();
	var upwd=$('r_upwd').val();
	var rupwd=$('r_cupwd').val();
	var remail=$('r_email').val();
	var patrn_name=/^([a-zA-Z0-9][._]){6,20}$/;
	var patrn_upwd=/^([a-zA-Z0-9][._]){6,20}$/;
	if(!patrn_name.exec(uname))
	{
		layer.tips("用户名输入有误！只能输入6-20个数字字母或者._,请重新输入！","#r_uname",{
			tips:2,
			time:1000
		})
		return;
	}
	if(patrn_upwd.exec(upwd)){
		layer.tips("密码输入有误！只能输入6-12个数字字母或者._,请重新输入！","#r_upwd",{
			tips:2,
			time:1000
		})
		return ;
	}
	if(upwd!=upwd){
		layer.tips("密码与确认密码不一致！","r_cupwd",{
			tips:2,
			time:1000
		})
		return;	
	}
	var patrn_e= /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
	if(!patrn_e,exec(remail)){
		layer.tips("邮箱格式不正确","r_email",{
			tips:2,
			time:1000
		})
		return;
	}
	$.jaxa({
		type:"get",
		url:"dataJson/loginregister.json",
		data:{},
		dataType:"json",
		success:function(result){
			var temp=result.code;
			var data=result.data.msg;
			if(temp==1){
				layer.msg(data,{icon:1,time:1000});
			}
			else{
				layer.msg("注册失败!",{icon:2});
			}
		},
		error:function(){
			layer.msg("访问服务器失败！",{icon:2});
		}
		
	});	
}



